unit UltimateSheduler_ActionGroupForm;

interface

uses
  UltimateSheduler_Defines,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, Grids, StdCtrls, ExtCtrls, CheckLst, XPMan;

type
  TUSActionGroupForm = class(TForm)
    LE_Title: TLabeledEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    CLB_Actions: TCheckListBox;
    Label2: TLabel;
    SB_AddAct: TSpeedButton;
    SB_DelAct: TSpeedButton;
    SB_ActUp: TSpeedButton;
    SB_ActDn: TSpeedButton;
    SB_EditAct: TSpeedButton;
    XPManifest1: TXPManifest;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure LE_TitleChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CLB_ActionsClick(Sender: TObject);
    procedure SB_ActUpClick(Sender: TObject);
    procedure SB_ActDnClick(Sender: TObject);
    procedure SB_AddActClick(Sender: TObject);
    procedure CLB_ActionsDblClick(Sender: TObject);
    procedure SB_EditActClick(Sender: TObject);
    procedure SB_DelActClick(Sender: TObject);
    procedure CLB_ActionsClickCheck(Sender: TObject);
  private
    { Private declarations }
    FActionGroup: ActionGroup;
    Lang: String;
    sDeleteCond: String;
    sDeleteAct: String;
    sCondNoEmpty: String;
    sActNoEmpty: String;
    FActionTitles: String;
    procedure SetLanguage(language: String);
    procedure TranslateToo;
    procedure SetActionGroup(anActionGroup: ActionGroup);
    procedure BitBtnClick(BitBtn: TBitBtn);
    procedure UpdateMyActions;
    procedure CheckActButtons;
    function EditAction(Idx: Integer): Boolean;
  public
    { Public declarations }
    property Language: String write SetLanguage;
    property myActionGroup: ActionGroup read FActionGroup write SetActionGroup;
    property ActionTitles: String write FActionTitles;
  end;

implementation

{$R *.dfm}

uses
 gnugettext,
 Locales,
 formula,
 UltimateSheduler_ActionForm;

procedure TUSActionGroupForm.FormCreate(Sender: TObject);
begin
 FActionGroup:=ActionGroup.Create(nil);
 //
 TranslateComponent(Self);
 TranslateToo;
 //
 FActionTitles:='';
end;

procedure TUSActionGroupForm.SetLanguage(language: String);
begin
 Lang:=language;
 TranslateForm(Self, Language, TranslateToo);
end;

procedure TUSActionGroupForm.TranslateToo;
begin
 sDeleteCond:=_('Are you sure to delete condition %s ?');
 sDeleteAct:=_('Are you sure to delete action "%s %s"?');
 sCondNoEmpty:=_('Condition list cannot be empty!');
 sActNoEmpty:=_('Action list cannot be empty!');
end;

procedure TUSActionGroupForm.SetActionGroup(anActionGroup: ActionGroup);
begin
 FActionGroup.Assign(anActionGroup);
 //
 LE_Title.Text:=FActionGroup.Title;
 UpdateMyActions;
end;

procedure TUSActionGroupForm.BitBtnClick(BitBtn: TBitBtn);
begin
 Self.ModalResult:=BitBtn.ModalResult;
 if not (fsModal in Self.FormState) then
   Self.Close;
end;

procedure TUSActionGroupForm.BitBtn1Click(Sender: TObject);
begin
 BitBtnClick(BitBtn1);
end;

procedure TUSActionGroupForm.BitBtn2Click(Sender: TObject);
begin
 BitBtnClick(BitBtn2);
end;

procedure TUSActionGroupForm.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
 if ModalResult=mrOK then
  begin
   CanClose:=CheckTitle(Self, LE_Title, FActionTitles);
   if not CanClose then
     Exit;
   //
   if FActionGroup.Actions.Count=0 then
    begin
     MessageDlg(sActNoEmpty, mtError, [mbOK], 0);
     CanClose:=false;
     Exit;
    end;
  end;
end;

procedure TUSActionGroupForm.LE_TitleChange(Sender: TObject);
begin
 FActionGroup.Title:=LE_Title.Text;
end;

procedure TUSActionGroupForm.FormDestroy(Sender: TObject);
begin
 FreeAndNil(FActionGroup);
end;

procedure TUSActionGroupForm.UpdateMyActions;
 var
  i, j: Integer;
begin
 CLB_Actions.Clear;
 for i:=0 to FActionGroup.Actions.Count-1 do
  begin
   j:=CLB_Actions.Items.Add(GetActTypeDesc(FActionGroup.Actions[i].ActType)+' '+FActionGroup.Actions[i].Destination);
   CLB_ACtions.Checked[j]:=FActionGroup.Actions[i].Enabled;
  end;
 CheckActButtons;
end;

procedure TUSActionGroupForm.CLB_ActionsClick(Sender: TObject);
begin
 CheckActButtons;
end;

procedure TUSActionGroupForm.CheckActButtons;
 var
  i: Integer;
begin
 i:=CLB_Actions.ItemIndex;
 SB_DelAct.Enabled:=i>=0;
 SB_EditAct.Enabled:=i>=0;
 SB_ActUp.Enabled:=i>0;
 SB_ActDn.Enabled:=i<FActionGroup.Actions.Count-1;
end;

procedure TUSActionGroupForm.SB_ActUpClick(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB_Actions.ItemIndex;
 if i>0 then
   FActionGroup.Actions.Items[i].Index:=i-1;
 UpdateMyActions;
end;

procedure TUSActionGroupForm.SB_ActDnClick(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB_Actions.ItemIndex;
 if i<FActionGroup.Actions.Count-1 then
   FActionGroup.Actions.Items[i].Index:=i+1;
 UpdateMyActions;
end;

procedure TUSActionGroupForm.SB_AddActClick(Sender: TObject);
begin
 if EditAction(FActionGroup.Actions.Count) then
   UpdateMyActions;
end;

procedure TUSActionGroupForm.CLB_ActionsDblClick(Sender: TObject);
begin
 if EditAction(CLB_Actions.ItemIndex) then
   UpdateMyActions;
end;

procedure TUSActionGroupForm.SB_EditActClick(Sender: TObject);
begin
 if EditAction(CLB_Actions.ItemIndex) then
   UpdateMyActions;
end;

procedure TUSActionGroupForm.SB_DelActClick(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB_Actions.ItemIndex;
 if MessageDlg(Format(sDeleteAct, [GetActTypeDesc(FActionGroup.Actions[i].ActType), FActionGroup.Actions[i].Destination]), mtConfirmation, [mbYes, mbNo], 0)=mrYes then
  begin
   FActionGroup.Actions.Delete(i);
   UpdateMyActions;
  end;
end;

function TUSActionGroupForm.EditAction(Idx: Integer): Boolean;
 var
  New: Boolean;
  newAction: anAction;
  AF: TUSActionForm;
begin
 AF:=TUSActionForm.Create(Self);
 AF.Language:=Lang;
 if (Idx>=0) then
  begin
   if Idx>=FActionGroup.Actions.Count then
    begin
     newAction:=FActionGroup.Actions.Add;
     newAction.Clear;
     AF.myAction:=newAction;
     New:=true;
     Idx:=newAction.Index;
    end
   else
    begin
     AF.myAction:=FActionGroup.Actions[Idx];
     New:=false;
    end;
   Result:=AF.ShowModal=mrOK;
   if Result then
     FActionGroup.Actions[Idx].Assign(AF.myAction)
   else
    begin
     if New then
       FActionGroup.Actions.Delete(FActionGroup.Actions.Count-1);
    end;
   FreeAndNil(AF);
  end
 else
   Result:=false;
end;

procedure TUSActionGroupForm.CLB_ActionsClickCheck(Sender: TObject);
 var
  i: Integer;
begin
 i:=CLB_Actions.ItemIndex;
 FActionGroup.Actions[i].Enabled:=CLB_Actions.Checked[i];
end;

end.
